import 'dart:math';

import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';

/// @Author wywinstonwy
/// @Date 2022/10/25 09:03
/// @Description: 显示动画

class RotationAnimationPage extends StatefulWidget {
  const RotationAnimationPage({Key? key}) : super(key: key);

  @override
  State<RotationAnimationPage> createState() => _RotationAnimationPageState();
}

class _RotationAnimationPageState extends State<RotationAnimationPage> with SingleTickerProviderStateMixin{
  late AnimationController _controller;
  late Animation<double> _turns;
  bool _playing = false;
  //控制动画状态
  void _toggle(){
    if(_playing){
      _playing = false;
      _controller.stop();
    }else{
      _controller.forward()..whenComplete(() => _controller.reverse());
      _playing = true;
    }
    setState(() {
      
    });
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //初始化动画控制器，设置动画时间
    _controller = AnimationController(vsync: this,duration: Duration(seconds: 10));
    //设置动画取值范围和时间曲线
    _turns = Tween(begin: 0.0, end: pi * 2).animate(
      CurvedAnimation(parent: _controller, curve: Curves.easeIn),
    );
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar("显示动画"),
      body: Center(
        child: RotationTransition(
          turns: _turns,
          child:  Container(
            width: 200,
            height: 200,
            child: Image.asset("images/室内_风扇02.png",fit: BoxFit.cover,),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(onPressed: _toggle,
      child: Icon(_playing ? Icons.pause : Icons.play_arrow),
      ),
    );
  }
}
